package action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import entity.Question;
import entity.Survey;
import entity.User;
import mapper.UserMapper;
import service.SurveyService;
import service.UserService;



@Controller
public class UserAction {
	@Autowired
	private UserService userService;
	
	@Autowired
	private SurveyService surveyService;
	
	@RequestMapping("/login.do")
	public String login(String user_name,String user_password, HttpServletRequest request,Model model) {
		User user=new User();
		user.setUser_name(user_name);
		user.setUser_password(user_password);
		User users=userService.login(user);
		if(users!=null) {
			HttpSession session = request.getSession();
			session.setAttribute("users", users);
			return "redirect:/home.do";
		}else {
			String emg="用户名或密码错误！";
			model.addAttribute("emg", emg);
			return "login";
		}
		
	}
	
	@RequestMapping("/home.do")
	public ModelAndView welcome(HttpServletRequest request){
		Survey survey = new Survey();
		User user = new User();
		HttpSession session = request.getSession();
		user = (User) session.getAttribute("users");
		survey.setUser_name(user.getUser_name());
		Map<String,Object> model = new HashMap<String,Object>();
		List<Survey> surveys = surveyService.findAllSurvey(survey);
		if(surveys.size()==0) {
			return new ModelAndView("home");
		}
		model.put("surveys", surveys);
		return new ModelAndView("home",model);
	}
	
	@RequestMapping(value="/sign.do",method = RequestMethod.POST)
	public String sign(String user_name,String user_password,String user_age,@RequestParam(value="photo") MultipartFile photo,HttpServletRequest request) {
		User user=new User();
		user.setUser_name(user_name);
		user.setUser_password(user_password);
		user.setUser_age(Integer.parseInt(user_age));
		user.setUser_root(0);
		
		// 取得绝对位置
		ServletContext application = request.getServletContext();
		String realPath = application.getRealPath("images/user");
		// 重新命名上传文件
		int index = photo.getOriginalFilename().lastIndexOf(".");
		String suffix = photo.getOriginalFilename().substring(index + 1);
		String fileName = realPath + File.separator + user_name + "." + suffix;
		
		System.out.println(fileName);
		// 存储照片到photo文件夹中
		try {
			photo.transferTo(new File(fileName));
		} catch (Exception e) {
			e.printStackTrace();
		}
		user.setUser_photo(suffix);

		boolean users=userService.sign(user);

		if(users) {
			return "login";
		}else {
			return "sign";
		}
	}
	
	//管理员注册
	@RequestMapping(value="/adminSign.do",method = RequestMethod.POST)
	public String adminSign(String user_name,String user_password,String user_age,@RequestParam(value="photo") MultipartFile photo,HttpServletRequest request) {
		User user=new User();
		user.setUser_name(user_name);
		user.setUser_password(user_password);
		user.setUser_age(Integer.parseInt(user_age));
		user.setUser_root(Integer.parseInt(request.getParameter("user_root")));
		
		// 取得绝对位置
		ServletContext application = request.getServletContext();
		String realPath = application.getRealPath("images/user");
		// 重新命名上传文件
		int index = photo.getOriginalFilename().lastIndexOf(".");
		String suffix = photo.getOriginalFilename().substring(index + 1);
		String fileName = realPath + File.separator + user_name + "." + suffix;
		
		System.out.println(fileName);
		// 存储照片到photo文件夹中
		try {
			photo.transferTo(new File(fileName));
		} catch (Exception e) {
			e.printStackTrace();
		}
		user.setUser_photo(suffix);

		boolean users=userService.sign(user);

		if(users) {
			return "redirect:/adminView.do";
		}else {
			return "redirect:/adminView.do";
		}
	}
	
	//注销方法
    @RequestMapping("/outLogin.do")
    public String outLogin(HttpSession session){
        //通过session.invalidata()方法来注销当前的session
        session.invalidate();
        return "login";
    }
    
    @RequestMapping("/adminView.do")
	public ModelAndView adminView(HttpServletRequest request){
    		Map<String,Object> model = new HashMap<String,Object>();
		List<User> users = userService.findAllUser();
		if(users.size()==0) {
			return new ModelAndView("adminView");
		}else {
			model.put("users", users);
		}
		return new ModelAndView("adminView",model);
    }
    
    @RequestMapping("/userDelet.do")
	public String surveyDelet(HttpServletRequest request) {
		User user = new User();
		String userIdTemp=request.getParameter("user_id");
		int userId=Integer.parseInt(userIdTemp);
		
		user.setUser_id(userId);
		System.out.println(user.getUser_id());
		
		userService.deleteUserById(user);
		
		return "redirect:/adminView.do";
	}

}
